import { queryContent } from "@/api/base";

class Dic {
    /**
     * 字典类型
     * @param {String} dicType 
     */
    constructor (dicType) {
        this.dicType = dicType;
        this.state = [
            { label: '1', value: '' }
        ];
        this.state = null;
    }

    init() {
        return queryContent(this.dicType).then((result) => {
            this.state = result.content.map(i => ({ label: i.dictValue, value: i.dictKey }));
            return this.state;
        });
    }

    /**
     * 获取显示内容
     * @param {*} value 
     * @returns 
     */
    getLabel(value) {
        if (!this.state) {
            this.init();
        }
        const item = this.state.find(i => i.value == value);
        return item ? item.label : null;
    }

    /**
     * 获取选项
     * @param {Number} type 0.常规 1.带额外全部选项
     * @returns {Promise<{}[]>}
     */
    getOpts(type) {
        if (!this.state) {
            return this.init().then(() => this.getOpts(type));
        }
        const val = this.state.filter(i => i.label != '未知');
        if (type == 1) {
            val.unshift({ label: '全部', value: null });
        }
        return Promise.resolve(val);
    }
}
/**
 * 性别
 */

export const sexDic = [
    {
      "dictKey": "0",
      "dictValue": "男"
    },
    {
      "dictKey": "1",
      "dictValue": "女"
    },
    {
      "dictKey": "2",
      "dictValue": "未知"
    }
  ];
// new Dic('sys_user_sex');
/**
 * 政治面貌
 */
export const politicalAffiliationDic = new Dic('political_affiliation');
 
/**
 *  民族
 */
export const nationDic = new Dic('nation');

/**
 *  M职级
 */
export const mLevelDic = new Dic('emp_m_level');

/**
 *  P职级
 */
export const pLevelDic = new Dic('emp_p_level');

/**
 *  M职位
 */
export const mPostDic = new Dic('emp_m_post');

/**
 *  P职位
 */
export const pPostDic = new Dic('emp_p_post');